package com.neuedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neuedu.pojo.User;
import com.neuedu.service.IMailService;
import com.neuedu.service.IUserService;
import com.neuedu.util.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author wgq
 * @since 2020-09-18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    BCryptPasswordEncoder encoder;
    @Resource
    IUserService userService;
    @Resource
    RedisTemplate<String, String> redisTemplate;
    @Resource
    IMailService mailService;

    @GetMapping("/list")
    CommonResult list(User user) {
        return CommonResult.success(userService.list(user));
    }

    @PostMapping("/add")
    CommonResult add(User user) {
        user.setPassword(encoder.encode(user.getPassword()));
        user.setActive(1);
        if (userService.add(user))
            return CommonResult.success("ok");
        else
            return CommonResult.failed("用户名已经存在");
    }

    @GetMapping("/getById")
    CommonResult getById(Integer id) {
        User user = userService.getById(id);
        user.setPassword("");
        return CommonResult.success(user);
    }

    @PostMapping("/update")
    CommonResult update(User user) {
        user.setPassword(encoder.encode(user.getPassword()));
        return CommonResult.success(userService.updateById(user));
    }

    @PostMapping("/del")
    CommonResult del(User user) {
        user.setActive(0);
        return CommonResult.success(userService.updateById(user));
    }

    @PostMapping("/batchDel")
    CommonResult batchDel(Integer[] ids) {
        return CommonResult.success(userService.batchDel(ids));
    }

    @PostMapping("/back")
    CommonResult back(User user) {
        user.setActive(1);
        return CommonResult.success(userService.updateById(user));
    }

    @RequestMapping("/token")
    CommonResult login(User user) throws JsonProcessingException {
//        return CommonResult.success();
        String token = userService.token(user);
        if (StringUtils.isNotBlank(token)) {
            return CommonResult.success(token);
        } else {
            return CommonResult.failed("用户名或密码错误");
        }
    }

    @RequestMapping("/logout")
    CommonResult logout(HttpServletRequest request) {
        return CommonResult.success(userService.logout(request));
    }

    @GetMapping("/currentUser")
    CommonResult currentUser(HttpServletRequest request) {
        return CommonResult.success(userService.currentUser(request));
    }

    @PostMapping("/changePassword")
    CommonResult changePassword(String username,
                                String email,
                                String securityCode,
                                String password,
                                String confirm) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userService.getOne(wrapper);
        if (user == null) {
            return CommonResult.failed("用户不存在");
        }
        if (!user.getEmail().equals(email)) {
            return CommonResult.failed("邮箱错误");
        }
        if (userService.changePassword(username, securityCode, password)) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed("验证码错误");
        }
    }
}
